(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('ratingTypeService', ratingTypeService);
	ratingTypeService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'gridService', 'modalService' ];
	function ratingTypeService($q, loggerFactory, commonService, ajaxServiceFactory, gridService, modalService) {
		var log = loggerFactory.getLogger("ratingTypeService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			dropDownDataOptions : {
				url : 'do/cda/ratingType/dropDownDataOptions',
				method : 'post'
			}
		});
		function getRuleTypeOptions() {
			var options = {
				"callback" : {
					"changed" : "vm.ruleTypeChanged"
				},
				"id" : "cda.ratingQaRuleType",
				"boundTo" : {
					"qaitemRuletype" : "qaitemRuletype",
					"itemRuletypeParams" : "qaitemRuletypeParams"
				},
				"okEvent" : true,
				"cancelEvent" : false
			};
			return options;
		}
		function dropDownDataOptions() {
			var defer = $q.defer();
			ajaxService.dropDownDataOptions().then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		function initData($scope, ctrl, updateVerifyFlag) {
			var defer = $q.defer();
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.ratingGrades.tableConfigs.table.validatorConfig = ctrl.validatorConfig.ratingGrades;
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.ratingQaItems.tableConfigs.table.validatorConfig = ctrl.validatorConfig.ratingQaItems;
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.ratingQaAdjs.tableConfigs.table.validatorConfig = ctrl.validatorConfig.ratingQaAdjs;
			ctrl.innerGridGradeOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ratingGrades.tableConfigs.table, rowSelectionChangedOfGrade);
			ctrl.innerGridGradeOptions.data = 'vm.ratingType.ratingGrades';
			ctrl.innerGridQaItemOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ratingQaItems.tableConfigs.table, rowSelectionChangedOfQaItem);
			ctrl.innerGridQaItemOptions.data = 'vm.ratingType.ratingQaItems';
			ctrl.innerGridQaItemOptions.multiSelect = true;//defalut is true
			ctrl.innerGridQaItemOptions.enableSelectAll = true;//defalut is true
			ctrl.innerGridQaAdjOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ratingQaAdjs.tableConfigs.table, rowSelectionChangedOfQaAdj);
			ctrl.innerGridQaAdjOptions.data = 'vm.ratingType.ratingQaAdjs';
			ctrl.innerGridQaAdjOptions.multiSelect = true;//defalut is true
			ctrl.innerGridQaAdjOptions.enableSelectAll = true;//defalut is true
			if(updateVerifyFlag) {
				ctrl.innerGridGradeOptions.verifyData = 'vm.ratingTypeOld.ratingGrades';
				ctrl.innerGridQaItemOptions.verifyData = 'vm.ratingTypeOld.ratingQaItems';
				ctrl.innerGridQaAdjOptions.verifyData = 'vm.ratingTypeOld.ratingQaAdjs';
			}
			dropDownDataOptions().then(function(response) {
				ctrl.modelNameOptions = response.modelNameOptions;
				ctrl.ratingCatOptions = response.ratingCatOptions;
				ctrl.yesOrNoOptions = response.yesOrNoOptions;
				ctrl.scoreDecimalOptions = response.scoreDecimalOptions;
				ctrl.itemTypeOptions = response.itemTypeOptions;;
				defer.resolve();
			}, angular.noop);
			
			function rowSelectionChangedOfGrade(eventRow, selectedRows) {
				ctrl.selectedGrades = selectedRows;
			}
			ctrl.addQaItem = function() {
				var defer = $q.defer();
				if (!ctrl.ratingType.ratingQaItems) {
					ctrl.ratingType.ratingQaItems = [];
				}
				var item = {
					ratingType : ctrl.ratingType.ratingType,
					qaitemDesc : '',
					isCategory : '',
					dispOrder : '',
					isRequired : '',
					itemType : '',
					parentQaitemId : ''
				};
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaItem',
					'params' : item,
					'btnCallback' : {
						'ok' : function(qaChoiceCtrl) {
							item = qaChoiceCtrl.ratingQaItem;
							item.dataStatus = 'C';
							item.ratingQaChoices = qaChoiceCtrl.ratingQaItem.ratingQaChoices;
							ctrl.ratingType.ratingQaItems.push(item);
						}
					}
				});
				defer.reject();
				return defer.promise;
			};

			ctrl.editQaItem = function(obj) {
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaItem',
					'params' : obj.entity,
					'btnCallback' : {
						'ok' : function(qaChoiceCtrl) {
							angular.forEach(ctrl.selectedQaItems, function(selectedItem, index) {
								angular.forEach(ctrl.ratingType.ratingQaItems, function(item, i) { 
									if (selectedItem.$$hashKey == item.$$hashKey) { 
										ctrl.ratingType.ratingQaItems[i].ratingQaChoices = qaChoiceCtrl.ratingQaItem.ratingQaChoices;
										if('C' != ctrl.ratingType.ratingQaItems[i].dataStatus) ctrl.ratingType.ratingQaItems[i].dataStatus = 'U';
									}
								});
							});
						}
					}
				});
			};
			function rowSelectionChangedOfQaItem(eventRow, selectedRows) {
				ctrl.selectedQaItems = selectedRows;
			}

			ctrl.addQaAdj = function() {
				var defer = $q.defer();
				if (!ctrl.ratingType.ratingQaAdjs) {
					ctrl.ratingType.ratingQaAdjs = [];
				}
				var item = {
					ratingType : ctrl.ratingType.ratingType,
					qaadjDesc : '',
					adjRule : '',
					adjCondition : '',
					dispOrder : ''
				};
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaAdj',
					'params' : item,
					'btnCallback' : {
						'ok' : function(qaAdjdCtrl) {
							item = qaAdjdCtrl.ratingQaAdj;
							item.dataStatus = 'C';
							item.ratingQaAdjds = qaAdjdCtrl.ratingQaAdj.ratingQaAdjds;
							ctrl.ratingType.ratingQaAdjs.push(item);
						}
					}
				});
				defer.reject();
				return defer.promise;
			};

			ctrl.editQaAdj = function(obj) {
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaAdj',
					'params' : obj.entity,
					'btnCallback' : {
						'ok' : function(qaAdjdCtrl) {
							angular.forEach(ctrl.selectedQaAdjs, function(selectedItem, index) {
								angular.forEach(ctrl.ratingType.ratingQaAdjs, function(item, i) { 
									if (selectedItem.$$hashKey == item.$$hashKey) { 
										ctrl.ratingType.ratingQaAdjs[i].ratingQaAdjds = qaAdjdCtrl.ratingQaAdj.ratingQaAdjds;
										if('C' != ctrl.ratingType.ratingQaItems[i].dataStatus) ctrl.ratingType.ratingQaItems[i].dataStatus = 'U';
									}
								});
							});
						}
					}
				});
			};
			function rowSelectionChangedOfQaAdj(eventRow, selectedRows) {
				ctrl.selectedQaAdjs = selectedRows;
			}
			//for view
			ctrl.viewQaItem = function(obj) {
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaItemView',
					'params' : obj.entity,
					'btnCallback' : {
						'ok' : function(qaChoiceCtrl) {
							angular.forEach(ctrl.selectedQaItems, function(selectedItem, index) {
								angular.forEach(ctrl.ratingType.ratingQaItems, function(item, i) { 
									if (selectedItem.$$hashKey == item.$$hashKey) { 
										ctrl.ratingType.ratingQaItems[i].ratingQaChoices = qaChoiceCtrl.ratingQaItem.ratingQaChoices;
									}
								});
							});
						}
					}
				});
			};

			ctrl.viewQaAdj = function(obj) {
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.ratingType.qaAdjView',
					'params' : obj.entity,
					'btnCallback' : {
						'ok' : function(qaAdjdCtrl) {
							angular.forEach(ctrl.selectedQaAdjs, function(selectedItem, index) {
								angular.forEach(ctrl.ratingType.ratingQaAdjs, function(item, i) { 
									if (selectedItem.$$hashKey == item.$$hashKey) { 
										ctrl.ratingType.ratingQaAdjs[i].ratingQaAdjds = qaAdjdCtrl.ratingQaAdj.ratingQaAdjds;
									}
								});
							});
						}
					}
				});
			};
			return defer.promise;
		}
		return {
			getRuleTypeOptions : getRuleTypeOptions,
			dropDownDataOptions : dropDownDataOptions,
			initData : initData
		};
	}
})(angular);